package com.byght.bygsystem.controller;

import com.byght.bygsystem.model.Role;
import com.byght.bygsystem.model.UserRole;
import com.byght.bygsystem.model.UserRoleDTO;
import com.byght.bygsystem.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/user-roles")
public class UserRoleController {
    @Autowired
    private UserRoleService userRoleService;
    // 分配角色给用户
    @PostMapping("/assign")
    public UserRole assignRoleToUser(@RequestParam Long userId, @RequestParam Long roleId) {
        return userRoleService.assignRoleToUser(userId, roleId);
    }

    @PostMapping
    public ResponseEntity<UserRole> assignRoleToUser(@RequestBody UserRoleDTO userRoleDTO) {
        UserRole userRole = userRoleService.assignRoleToUser(userRoleDTO.getUserId(), userRoleDTO.getRoleId());
        return new ResponseEntity<>(userRole, HttpStatus.CREATED);
    }

    @DeleteMapping
    public ResponseEntity<Void> removeRoleFromUser(@RequestParam Long userId, @RequestParam Long roleId) {
        userRoleService.removeRoleFromUser(userId, roleId);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @GetMapping("/{userId}")
    public ResponseEntity<List<Role>> getRolesByUserId(@PathVariable Long userId) {
        List<Role> roles = userRoleService.getRolesByUserId(userId);
        return new ResponseEntity<>(roles, HttpStatus.OK);
    }
}
